<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2022/3/25
 */

namespace App\Http\Middleware;

use App\Services\ConfigService;
use Closure;
use Illuminate\Http\Request;

/**
 * （网站等）应用状态维护
 * Class AppMaintenance
 * @package App\Http\Middleware
 */
class AppMaintenance
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        // 网站状态检测
        $websiteStatus = ConfigService::getConfigItemValue('site', 'status');
        if (!$websiteStatus && !$request->expectsJson()) {
            $websiteName = ConfigService::getConfigItemValue('site', 'name');
            return response()->view('errors.down', ['site_name', $websiteName]);
        }
        return $next($request);
    }
}
